//
//  UIScrollMenu.m
//  QGSPos
//
//  Created by QGSVN42 on 6/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#if !__has_feature(objc_arc)
#error "This source file must be compiled with ARC enabled!"
#endif

#import "UIScrollMenu.h"
#import <QuartzCore/QuartzCore.h>
#import "AsynImageButton.h"

#define Number_Element 6
#define LEFT_MARGIN 80
#define TOP_MARGIN 30
@implementation UIScrollMenu
@synthesize parent;
@synthesize scrollView;
@synthesize pageControl;
@synthesize arrayMenu;
- (id)initWithFrame:(CGRect)frame parent:(id)sender
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        parent = sender;
        self.backgroundColor = [UIColor clearColor];
        
        numberPage = 0;
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0,self.frame.size.height - 10,150,10)];
        pageControl.center = CGPointMake(self.frame.size.width/2,pageControl.center.y);
        pageControl.backgroundColor = [UIColor clearColor];
        //    [self addSubview:pageControl];
        [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-10)];
        [self addSubview:scrollView];
        [self setupPage];
    }
    return self;
}

#pragma mark The Guts
- (void)setupPage
{
    if([arrayMenu count] % Number_Element == 0)
        numberPage = [arrayMenu count] / Number_Element;
    else
        numberPage = ([arrayMenu count] / Number_Element) + 1;
    for (id subView in scrollView.subviews) {
        if(![subView isKindOfClass:[UIPageControl class]])
            [subView removeFromSuperview];
    }
	scrollView.delegate = self;
    
	[scrollView setCanCancelContentTouches:NO];
	scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
	scrollView.clipsToBounds = YES;
	scrollView.scrollEnabled = NO;
	scrollView.pagingEnabled = YES;

    //width button = 175, height = 173 (size image)
    //left = (1024 - (175*3) - 1 - 1)/2 = 248
    //3 is number column
    //1 is width line
    
	CGFloat cx = 248;
    CGFloat cy = 50;
    for (int i=0; i<numberPage; i++) {
        //size scroll 680 x 580
        //size button 136 x
        for (int j =0; j < Number_Element; j++) {
            if(i*Number_Element+j >= [arrayMenu count])
                break;
            NSMutableDictionary *dict = [arrayMenu objectAtIndex:i*Number_Element+j];
            
            AsynImageButton *btnMenu = [AsynImageButton buttonWithType:UIButtonTypeCustom];
            [btnMenu addTarget:parent action:@selector(btnMenuClick:) forControlEvents:UIControlEventTouchUpInside];
            [btnMenu setTitle:[dict objectForKey:CATEGORY_NAME] forState:UIControlStateNormal];
            [btnMenu setTitleColor:APPLE_BLUE_COLOR forState:UIControlStateNormal];
            btnMenu.titleLabel.font = [UIFont systemFontOfSize:14];
            [btnMenu setTitleShadowColor:[UIColor darkTextColor] forState:UIControlStateNormal];
            [btnMenu setBackgroundImage:[UIImage imageNamed:[dict objectForKey:CATEGORY_IMAGE]] forState:UIControlStateNormal];
            [btnMenu setBackgroundImage:[UIImage imageNamed:[dict objectForKey:CATEGORY_IMAGE_SELECTED]] forState:UIControlStateHighlighted];
            [btnMenu setContentEdgeInsets:UIEdgeInsetsMake(0, 0, -135, 0)];
            int row = j/3;
            int column = j%3;
            if(column > 0 && row == 0)
            {
                UILabel *lbLine = [[UILabel alloc] initWithFrame:CGRectMake(cx + column*175, 50, 1, 173*2 + 1)];
                lbLine.backgroundColor = [UIColor lightGrayColor];
                [scrollView addSubview:lbLine];
                
                cx += 1;
            }
            if(row > 0 && column == 0)
            {
                UILabel *lbLine = [[UILabel alloc] initWithFrame:CGRectMake(248, row*173+cy, 175*3 + 2, 1)];
                lbLine.backgroundColor = [UIColor lightGrayColor];
                [scrollView addSubview:lbLine];
                
                cy += 1;
            }
            
            btnMenu.frame = CGRectMake(cx + column*175,row*173+cy, 175, 173);
            //            btnMenu.tag = i*Number_Element+j + 50;
            btnMenu.tag = [[dict objectForKey:@"index"] intValue];
            [scrollView addSubview:btnMenu];
        }
        cx += scrollView.frame.size.width;
    }
//    CGFloat cx = LEFT_MARGIN;
//    for (int i=0; i<numberPage; i++) {
//        //size scroll 680 x 580
//        //size button 136 x
//        for (int j =0; j < Number_Element; j++) {
//            if(i*Number_Element+j >= [arrayMenu count])
//                break;
//            NSMutableDictionary *dict = [arrayMenu objectAtIndex:i*Number_Element+j];
//            
//            AsynImageButton *btnMenu = [AsynImageButton buttonWithType:UIButtonTypeCustom];
//            [btnMenu addTarget:parent action:@selector(btnMenuClick:) forControlEvents:UIControlEventTouchUpInside];
//            [btnMenu setTitle:[dict objectForKey:CATEGORY_NAME] forState:UIControlStateNormal];
//            [btnMenu setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//            btnMenu.titleLabel.font = [UIFont systemFontOfSize:15];
//            [btnMenu setTitleShadowColor:[UIColor darkTextColor] forState:UIControlStateNormal];
//            [btnMenu setBackgroundImage:[UIImage imageNamed:[dict objectForKey:CATEGORY_IMAGE]] forState:UIControlStateNormal];
//            [btnMenu setBackgroundImage:[UIImage imageNamed:[dict objectForKey:CATEGORY_IMAGE_SELECTED]] forState:UIControlStateHighlighted];
//            [btnMenu setContentEdgeInsets:UIEdgeInsetsMake(0, 0, -135, 0)];
//            int row = j/4;
//            int column = j%4;
//            btnMenu.frame = CGRectMake(cx + column*232,row*194+TOP_MARGIN, 152, 164);
////            btnMenu.tag = i*Number_Element+j + 50;
//            btnMenu.tag = [[dict objectForKey:@"index"] intValue];
//            [scrollView addSubview:btnMenu];
//        }
//        cx += scrollView.frame.size.width;
//    }
    
	self.pageControl.numberOfPages = numberPage;
    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}

#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;    
    
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    pageControlIsChangingPage = YES;
}

@end
